Write a C++ program to demonstrate the use of Friend function in class

 

              Assignment 13

Write a C++ program to demonstrate the use of Friend function in class.

: Example 1 :

Program Code with explanation:

1.      #include <iostream>   

2.      using namespace std;   

3.      class Demo    

4.      {   

5.                 int n1;   

6.          public:   

7.                friend int DemoFriend(Demo); //friend function   

8.      };   

9.      int DemoFriend(Demo b)   

10.   {   

11.             b.n1 = 100;   

12.             return b.n1;   

13.   }   

14.   int main()   

15.   {   

16.             Demo b;   

17.             cout << " Value of n1 of Class Demo: " << DemoFriend(b) << endl;   

18.             return 0;   

19.   }   

Explanation of Code:

Syntax of Friend function:

friend return_data_type function_name(list of arguments);  

Example:

           friend int DemoFriend(Demo object);

Define the friend function outside the class scope so friend function called as normal function.

It permits to access private and protected member of class. It access the member using object name.

We pass the object of class as a parameter to friend function for access member of class in friend function.    

Program Code for run:

#include <iostream>    

using namespace std;   

class Demo    

{   

        int n1;   

    public:   

        friend int DemoFriend(Demo); //friend function   

};   

int DemoFriend(Demo b)   

{   

        b.n1 = 100;   

        return b.n1;   

}   

int main()   

{   

        Demo b;   

        cout << " Value of n1 of Class Demo: " <<  DemoFriend(b) << endl;   

        return 0;   

}   

Output of Program:

Value of n1 of Class Demo: 100

 : Example 2 :

Program Code with explanation:

1.      #include <iostream> 

2.      using namespace std; 

3.      class Demo_B;          // Declarartion of class Demo_B. 

4.      class Demo_A 

5.      { 

6.                            int a; 

7.               public: 

8.                void accept(int i) 

9.                { 

10.                         a = i; 

11.             } 

12.             friend void min(Demo_A,Demo_B);        

13.             // friend function decleration. 

14.   }; 

15.   class Demo_B 

16.   { 

17.                         int b; 

18.                  public: 

19.                         void accept(int i) 

20.                         { 

21.                                     b = i; 

22.                         } 

23.                         friend void min(Demo_A,Demo_B);                   

24.                         // friend function decleration.

25.   }; 

26.   void min(Demo_A a1,Demo_B b1) 

27.   { 

28.                         cout << " Minimum Value from member of class";

29.                         cout << " Demo_A and Demo_B : ";

30.                         if(a1.a <= b1.b)

31.                                     cout << a1.a << endl; 

32.                         else 

33.                                     cout << b1.b << endl; 

34.   } 

35.   int main() 

36.   { 

37.                         Demo_A a; 

38.                         Demo_B b; 

39.                         a.accept(10); 

40.                         b.accept(20); 

41.                         min(a,b); 

42.                         return 0; 

43.   } 

 Explanation of Code:

    min function is friend function for both class Demo_A and Demo_B. 
            This min friend function can access private as well as protected member of both class using corresponding class object.
                We can pass object of class to friend function, Whose members want to access.

Program Code for run:

#include <iostream> 

using namespace std; 

class Demo_B;          // Declarartion of class Demo_B. 

class Demo_A 

            int a; 

      public: 

            void accept(int i) 

            { 

                        a = i; 

            } 

            friend void min(Demo_A, Demo_B);        

// friend function decleration. 

}; 

class Demo_B 

            int b; 

     public: 

            void accept(int i) 

            { 

                        b = i; 

            } 

            friend void min(Demo_A, Demo_B);                   

// friend function decleration.

}; 

void min(Demo_A a1, Demo_B b1) 

            cout << " Minimum Value from member of class";

cout << " Demo_A and Demo_B : ";

            if(a1.a <= b1.b)

                        cout << a1.a << endl; 

            else 

                        cout << b1.b << endl; 

int main() 

            Demo_A a; 

            Demo_B b; 

            a.accept(10); 

            b.accept(20); 

            min(a,b); 

            return 0; 

 } 

Output of Program:

                        Minimum Value from member of class Demo_A and Demo_B : 10

            

Comments